package com.ihcy.base.service.impl;

import com.ihcy.base.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class RedisServiceImpl implements RedisService {

    private static final String REDIS_LOCK = "lock";

    private final RedisTemplate redisTemplate;


    public RedisServiceImpl(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public Boolean lock(String key, String requestId) {
        return redisTemplate.opsForValue().setIfAbsent(key, requestId, 15, TimeUnit.SECONDS);
    }

    @Override
    public Boolean unlock(String key, String requestId) {

        return redisTemplate.delete(key);
    }
}
